<?php
class DB {
	private $driver;
	
	public function __construct($driver, $hostname, $username, $password, $database) {
		if (file_exists(DIR_DATABASE . $driver . '.php')) {
			require_once(VQMod::modCheck(DIR_DATABASE . $driver . '.php'));
		} else {
			exit('Error: Could not load database file ' . $driver . '!');
		}


		$this->driver = new $driver($hostname, $username, $password, $database);
		
	}
		
  	public function query($sql) {
		return $this->driver->query($sql);
  	}

  	public function sp_query($sql) {
  		return $this->driver->sp_query($sql);
  	}
  	 
  	
	public function escape($value) {
		return $this->driver->escape($value);
	}
	
  	public function countAffected() {
		return $this->driver->countAffected();
  	}

  	public function getLastId() {
		return $this->driver->getLastId();
  	}	
  	
  	/*
  	 * DAINX
  	*/
  	public function executesp($spname, $params = null){
  		if($params){
  			$paramString = '';
  			foreach($params as $param){
  				$paramString .= '\''.$param.'\',';
  			}
  			 
  			$paramString = substr($paramString, 0, strlen($paramString) - 1);
  			$query = "CALL `" . $spname . "`(" . $paramString . ")";
  			//
  			$results = $this->query($query);
  		}
  		else{
  			$results = $this->query("CALL `" . $spname . "`()");
  		}
  		return  json_decode(json_encode($results), true);
  		
  	}
}
?>